/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.query;

import nfse.*;

public class Criteria implements Comparable<Criteria> {
	public int field;
	public int predicate;
	public String value1;
	public String value2;

	public Criteria() {
	}

	public int compareTo(Criteria c) {
		try {
			if (c.predicate != predicate)
				return -1;
			if (c.field != field)
				return -1;
			if (c.value1.compareTo(value1) != 0)
				return -1;
			if ((c.value2 == null) && (value2 != null))
				return -1;
			if ((c.value2 != null) && (value2 == null))
				return -1;
			if (c.value2.compareTo(value2) == 0)
				return -1;
		} catch (Exception E) {
			return -1;
		}
		return 0;
	}

	public String toString() {
		String temp = "";
		temp += NetFSE.getFieldName(field) + "=\"";
		if (predicate == NetFSE.PREDICATE_RANGE)
			temp += value1 + NetFSE.getPredicateName(predicate) + value2;
		else if (predicate == NetFSE.PREDICATE_EQUAL_TO)
			temp += value1;
		else
			temp += NetFSE.getPredicateName(predicate) + value1;
		temp += "\"";
		return temp;
	}

	public String toXML() {
		String temp = "<SearchField ";
		temp += "field=\"" + field + "\" ";
		temp += "predicate=\"" + NetFSE.PREDICATES[predicate] + "\" ";
		if (predicate == NetFSE.PREDICATE_RANGE)
			temp += "lower=\"" + value1 + "\" upper=\"" + value2 + "\" ";
		else
			temp += "value=\"" + value1 + "\" ";
		temp += "/>";
		return temp;
	}

}